<?php namespace App\Http\Controllers\admin\finance;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use App\Models\Financing;
use DB;
use App\Models\Projects;
use App\Models\Devices;
use App\Models\Technologies;
use Redirect;
use Session;


class FinanceController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
		if(Session::has('admin')){
		return view("admin.finance.index")->withIsFinances(true);
	    }else{
	    	return Redirect::to('admin/dologin');
	    } 
	}

	public function addFinance()
	{
		//
		return view("admin.finance.add_finance")->withIsFinances(true);
	}

	public function editFinance()
	{
		//
		return view("admin.finance.edit_finance")->withIsFinances(true);
	}

	public function detailFinance()
	{
		return view("admin.finance.detail_finance")->withIsFinances(true);
	}

	


    public function loginOut()
    {
        if(Session::has('admin')){
            Session::forget('amind');
            return Redirect::to('login');
        }
    }


	public function getFinancing()
	{
		$fin_id = Input::get("fin_id");
		$model = Financing::with(["finIncome", "liability", "cashFlow"])->where("id", $fin_id);
		if($model->count()){
			$data = $model->first()->toArray();
			return $this->json(["data" => $data]);
		}else{
			return;
		}
	}
     
    

	public function projectList()
	{

		$current = (int)Input::get("current");
		$rowCount = (int)Input::get("rowCount");
		$sort = Input::get("sort")["id"];
		$sort = $sort ? $sort : "desc";
		$keywords = Input::get('searchPhrase');

		$model = Projects::with(['financing'])->where("is_financing", "!=", 0);
		$count = $model->get()->count();

		if($keywords){
			$project = $model->where("name", "like", "%".$keywords."%")->orWhere("description", "like", "%".$keywords."%")->skip(($current - 1)*$rowCount)->take($rowCount)->get();
		}else{
			$project = $model->skip(($current - 1)*$rowCount)->take($rowCount)->get();
		}
		foreach ($project as $key => $value) {
			$value["fin_id"] = $value["financing"]["id"];
		}
        return 1;
		return array(
			"current" => $current,
			"rowCount" => $rowCount,
			"rows" => $project,
			"total" => $count
			);
	}


	public function deviceList()
	{
		$current = (int)Input::get("current");
		$rowCount = Input::get("rowCount");
		$sort = Input::get("sort")["id"];
		$sort = $sort ? $sort : "desc";

		$keywords = Input::get('searchPhrase');

		$model = Devices::where("is_financing", "!=", 0);
		$count = $model->get()->count();

		if($keywords){
			$device = $model->where("name", "like", "%".$keywords."%")->orWhere("description", "like", "%".$keywords."%")->skip(($current - 1)*$rowCount)->take($rowCount)->get();
		}else{
			$device = $model->skip(($current - 1)*$rowCount)->take($rowCount)->get();
		}

		foreach ($device as $key => $value) {
			$value["fin_id"] = $value["financing"]["id"];
		}
		return array(
			"current" => $current,
			"rowCount" => $rowCount,
			"rows" => $device,
			"total" => $count
		);
	}

	public function techList()
	{
		$current = (int)Input::get("current");
		$rowCount = (int)Input::get("rowCount");
		$sort = Input::get("sort")["id"];
		$sort = $sort ? $sort : "desc";

		$keywords = Input::get('searchPhrase');

        $model= Technologies::where("is_financing", "!=", 0);
		$count = $model->get()->count();

		if($keywords){
			$tech = $model->where("tech_name", "like", "%".$keywords."%")->orWhere("description", "like", "%".$keywords."%")->skip(($current - 1)*$rowCount)->take($rowCount)->get();
		}else{
			$tech = $model->skip(($current - 1)*$rowCount)->take($rowCount)->get();
		}

		foreach ($tech as $key => $value) {
			$value["fin_id"] = $value["financing"]["id"];
		}

		return array(
			"current" => $current,
			"rowCount" => $rowCount,
			"rows" => $tech,
			"total" => $count
		);
	}





	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
